<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书名" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入书名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="bookType">
        <el-select v-model="queryParams.bookType" placeholder="状态" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.books_types_tag" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="状态" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.books_state_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="书名" align="center" prop="bookName" />
      <el-table-column label="图书类型" align="center" prop="bookType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.books_types_tag" :value="scope.row.bookType" />
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="bookBlurb" />
      <el-table-column label="状态" align="center" prop="state" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.books_state_type" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state != '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['books:borrow:query']"
          >借阅</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改领还记录表单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="图书名称" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入图书名称" :disabled="true"/>
        </el-form-item>
        <el-form-item label="借阅人" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入" :disabled="true"/>
        </el-form-item>
        <el-form-item label="书籍状态" prop="bookState">
          <el-select v-model="form.bookState" filterable placeholder="请选择书籍状态" :disabled="true">
            <el-option v-for="item in bookStates" :key="item.id" :label="item.dictLabel" :value="item.dictValue" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预计归还时间" prop="returnTime">
          <el-date-picker clearable
                          v-model="form.returnTime"
                          type="date"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择预计归还时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManage } from "@/api/books/manage";
import { selectBookInfo } from "@/api/books/borrow";
import { addRecord, getBookRecordStates, updateRecord } from '@/api/books/record'

export default {
  name: "Manage",
  dicts: ['books_state_type','books_types_tag','books_record_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图书信息表格数据
      manageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null,
        bookType: null,
        bookBlurb: null,
        state: null,
      },
      bookTypes: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      bookStates: [],
      openType:"out"
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图书信息列表 */
    getList() {
      this.loading = true;
      listManage(this.queryParams).then(response => {
        this.manageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bookName: null,
        bookType: null,
        bookBlurb: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getBookTypes("books_types_tag")
      this.open = true;
      this.title = "添加图书信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getBookRecordStates("books_record_state")
      const id = row.id
      selectBookInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "借阅信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.openType === "out"){//领取
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            })
          }else{//归还
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除图书名称为："' + row.bookName + '"的书籍信息吗？').then(function() {
        return delManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/manage/export', {
        ...this.queryParams
      }, `manage_${new Date().getTime()}.xlsx`)
    },
    getBookRecordStates (type) {
      getBookRecordStates(type).then(resp => {
        console.log(resp)
        this.bookStates = resp.rows;
      })
    },
  }
};
</script>
